using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using ResaGest.Metier.Entite.Client;
using ResaGest.DAO;
using ResaGest.Metier.Critere;

namespace ResaGest.DAO
{
    class clientDAO
    {
		public static List<Client> getAllClientByCritere(ClientCritere critere)
		{
			List<Client> lc = new List<Client>();
			string sqlCritere = "";
			if (!string.IsNullOrEmpty(critere.Adresse))
			{
				sqlCritere += " adresse_client like '" + critere.Adresse.Replace("'", "''") + "' and ";
			}
			if (!string.IsNullOrEmpty(critere.Cp))
			{
				sqlCritere += " cp_client like '" + critere.Cp.Replace("'", "''") + "' and ";
			}
			if (!string.IsNullOrEmpty(critere.Nom))
			{
				sqlCritere += " nom_client like '" + critere.Nom.Replace("'", "''") + "' and ";
			}
			if (!string.IsNullOrEmpty(critere.Prenom))
			{
				sqlCritere += "prenom_client like'" + critere.Prenom.Replace("'", "''") + "' and ";
			}
			if (!string.IsNullOrEmpty(critere.Ville))
			{
				sqlCritere += "ville_client like'" + critere.Ville.Replace("'", "''") + "' and ";
			}

			string requete = "";
			requete += sqlCritere != "" ? "select * from Client where " + sqlCritere.Substring(0, sqlCritere.LastIndexOf("and")) : "select * from Client";


			requSQL requ = new requSQL(requete);
			List<object> list = requ.getList();
			DataRow dr;
			Client cl = new Client();
			for (int i = 0; i < list.Count; i++)
			{
				dr = (DataRow)list[i];
				cl.Adresse = dr["adresse_client"] == System.DBNull.Value ? "" : (string)dr["adresse_client"];
				cl.Cp = dr["cp_client"] == System.DBNull.Value ? 0 : Convert.ToInt32(dr["cp_client"]);
				cl.Nom = dr["nom_client"] == System.DBNull.Value ? "" : (string)dr["nom_client"];
				cl.Prenom = dr["prenom_client"] == System.DBNull.Value ? "" : (string)dr["prenom_client"];
				cl.Ville = dr["ville_client"] == System.DBNull.Value ? "" : (string)dr["ville_client"];
				lc.Add(cl);
				cl = new Client();
			}


			return lc;

		}

		public static Client getClientById(int id)
		{
			requSQL requ = new requSQL("select * from Client where id = " + id);
			List<object> list = requ.getList();
			DataRow dr;
			Client cl = new Client();
			dr = (DataRow)list[0];
			cl.Adresse = dr["adresse_client"] == System.DBNull.Value ? "" : (string)dr["adresse_client"];
			cl.Cp = dr["cp_client"] == System.DBNull.Value ? 0 : (int)dr["cp_client"];
			cl.Nom = dr["nom_client"] == System.DBNull.Value ? "" : (string)dr["nom_client"];
			cl.Prenom = dr["prenom_client"] == System.DBNull.Value ? "" : (string)dr["prenom_client"];
			cl.Ville = dr["ville_client"] == System.DBNull.Value ? "" : (string)dr["ville_client"];

			return cl;
	}

    }
}
